import api from '@/api/api'

const config = {
  state: {
    data: JSON.parse(localStorage.getItem('template_configData')) || {}
  },
  mutations: {
    SET_CONFIG_DATA: (state, { key, value }) => {
      state.data[key] = value
      localStorage.setItem('template_configData', JSON.stringify(state.data))
    }
  },
  actions: {
    async batchGetConfig ({ commit }, codes) {
      for (let code of codes) {
        await api.findConfigByCode({
          data: {
            code: code
          }
        }).then(res => {
          commit('SET_CONFIG_DATA', { key: code, value: (res.success && res.data) ? res.data.val : '' })
        })
      }
    }
  }
}

export default config
